package com.icetec.iceintel.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.icetec.R;
import com.icetec.iceintel.model.Scheme;
import com.icetec.iceintel.netrequest.UniResAllocator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 道 on 2015/6/2.
 */
public class SchemeTypeAdapter extends BaseAdapter{

    private Context context;
    private LayoutInflater inflater;
    private List<String> schemeTypeList;
    private int choice;
    private SchemeType currentSchemeType;
    public SchemeTypeAdapter(Context context){
        this.context = context;
        inflater = LayoutInflater.from(this.context);
        schemeTypeList = new ArrayList<String>();
        schemeTypeList.add(Scheme.SchemeTypeWORK);
        schemeTypeList.add(Scheme.SchemeTypeRELAX);
        schemeTypeList.add(Scheme.SchemeTypeSLEEP);
        schemeTypeList.add(Scheme.SchemeTypeREAD);
        schemeTypeList.add(Scheme.SchemeTypeMOVIE);
        schemeTypeList.add(Scheme.SchemeTypeDIET);
        choice = 0;
    }

    @Override
    public int getCount() {
        return schemeTypeList.size();
    }

    @Override
    public String getItem(int item) {
        return schemeTypeList.get(item);
    }

    @Override
    public long getItemId(int item) {
        return 0;
    }

    public String getChoiceType(){
        return schemeTypeList.get(choice);
    }

    public void setChoice(int choice){
        this.choice = choice;
    }

    public void setChoice(String type){
        for(int i = 0; i < schemeTypeList.size(); i++){
            if(type.equals(schemeTypeList.get(i))){
                choice = i;
                break;
            }
        }
    }

    public void updateImage(View conertView){
        currentSchemeType.typeIamgeView.setImageDrawable(UniResAllocator.getSchemeResIDbyType(
                getItem(currentSchemeType.position), choice == currentSchemeType.position));
        SchemeType schemeType = (SchemeType) conertView.getTag();
        schemeType.typeIamgeView.setImageDrawable(UniResAllocator.getSchemeResIDbyType(getItem(schemeType.position), choice == schemeType.position));
        currentSchemeType = schemeType;
    }

    @Override
    public View getView(int position, View conertView, ViewGroup viewGroup) {
        final SchemeType schemeType;
        if(conertView == null){
            conertView = inflater.inflate(R.layout.commponent_schemetype_item, null);
            schemeType = new SchemeType();
            schemeType.position = position;
            schemeType.typeIamgeView = (ImageView)conertView.findViewById(R.id.scheme_type_iv);
            schemeType.typeTv = (TextView) conertView.findViewById(R.id.scheme_type_tv);
            conertView.setTag(schemeType);
        }else{
            schemeType = (SchemeType) conertView.getTag();
            schemeType.position = position;
        }

        if(position == choice){
            currentSchemeType = schemeType;
        }

        schemeType.typeIamgeView.setImageDrawable(UniResAllocator.getSchemeResIDbyType(getItem(position), choice == position));
        switch(position){
            case 0:
                schemeType.typeTv.setText(R.string.type_work);
                break;
            case 1:
                schemeType.typeTv.setText(R.string.type_relax);
                break;
            case 2:
                schemeType.typeTv.setText(R.string.type_sleep);
                break;
            case 3:
                schemeType.typeTv.setText(R.string.type_read);
                break;
            case 4:
                schemeType.typeTv.setText(R.string.type_movie);
                break;
            case 5:
                schemeType.typeTv.setText(R.string.type_diet);
                break;
            default:
                schemeType.typeTv.setText(R.string.type_work);
                break;
        }
        return conertView;
    }

    public class SchemeType{
        int position;
        ImageView typeIamgeView;
        TextView typeTv;
    }

}
